<?php
class Candidate_course_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }
    
    function get($data)
    {
    	$this->db->select('candidate_course.*, course_type.name AS typename, course_level.name AS levelname,
    					course_status.name AS statusname, country.name AS countryname, state.name AS statename');
    	$this->db->from('candidate_course');
    	$this->db->join('course_type', 'candidate_course.idcoursetype = course_type.id', 'left');
    	$this->db->join('course_level', 'candidate_course.idlevel = course_level.id', 'left');
    	$this->db->join('course_status', 'candidate_course.idstatus = course_status.id', 'left');
    	$this->db->join('country', 'candidate_course.idcountry = country.id', 'left');
    	$this->db->join('state', 'candidate_course.idstate = state.id', 'left');
    	
    	if (isset($data['idcandidate'])) {
    		$this->db->where('candidate_course.idcandidate', $data['idcandidate']);
    	}
    	
    	if (isset($data['idcourse']) && is_numeric($data['idcourse'])) {
    		$this->db->where('candidate_course.id', $data['idcourse']);
    	}
    	
    	if (isset($data['idlevel']) && is_numeric($data['idlevel'])) {
    		$this->db->where('candidate_course.idlevel', $data['idlevel']);
    	}
    	
    	if (isset($data['idstatus']) && is_numeric($data['idstatus'])) {
    		$this->db->where('candidate_course.idstatus', $data['idstatus']);
    	}
    	
    	if (isset($data['idcoursetype']) && is_numeric($data['idcoursetype'])) {
    		$this->db->where('candidate_course.idcoursetype', $data['idcoursetype']);
    	}
    	
    	if (isset($data['istitution']) && $data['istitution'] != '') {
    		$this->db->where('candidate_course.istitution', $data['istitution']);
    	}
    	
    	if (isset($data['startdate']) && $data['startdate'] != '') {
    		$this->db->where('candidate_course.startdate', $data['startdate']);
    	}
    	
    	$this->db->order_by('candidate_course.id');
    	
    	return $this->db->get();
    }
    
	function insert($data)
    {
    	$data_insert = array(
		   'idcountry' => $data['idcountry'],
		   'idstate' => $data['idstate'],
		   'idcoursetype' => $data['idcoursetype'],
		   'institution' => $data['institution'],
		   'idcandidate' => $data['idcandidate'],
		   'startdate' => $data['startdate'],
		   'finishdate' => $data['finishdate'],
		   'idcandidate' => $data['idcandidate'],
		   'idlevel' => $data['idlevel'],
		   'idstatus' => $data['idstatus']
		);
		
		return $this->db->insert('candidate_course', $data_insert);
    }
    
	function update($data)
    {
    	$data_insert = array(
		   'idcountry' => $data['idcountry'],
		   'idstate' => $data['idstate'],
		   'idcoursetype' => $data['idcoursetype'],
		   'institution' => $data['institution'],
		   'idcandidate' => $data['idcandidate'],
		   'startdate' => $data['startdate'],
		   'finishdate' => $data['finishdate'],
		   'idcandidate' => $data['idcandidate'],
		   'idlevel' => $data['idlevel'],
		   'idstatus' => $data['idstatus']
		);
		
		$this->db->where('id', $data['idcourse']);
		
		return $this->db->update('candidate_course', $data_insert);
    }
    
	function delete($data)
    {
    	$data_delete = array(
		   'id' => $data['id']
		);
		
		return $this->db->delete('candidate_course', $data_delete);
    }
}
?>